﻿using UnityEngine;
using UnityEngine.UI;

using System.Collections;
using DG.Tweening;

namespace VFramework
{
	public class VUICommonDisplay : MonoBehaviour
	{


		public GameObject tipNode;
		public VUILoadingIndicate loaddingIndicate;


		private static VUICommonDisplay _instance = null;

		private GameObject tipTmpNode = null;
		void Start ()
		{
			_instance = this;

		}


		/**
		创建一个tip样式，下面有一个名为t的text组件
		*/
		public static bool showTip (string msg)
		{
			if (msg == null)
				return false;


			if (_instance == null || _instance.tipNode == null) {
				Debug.Log ("you need set tip node !!");
				return false;
			}


			if (_instance.tipTmpNode != null) {
				GameObject.DestroyObject (_instance.tipTmpNode.gameObject);
			}

			GameObject tn = GameObject.Instantiate (_instance.tipNode);
			tn.transform.SetParent (_instance.tipNode.transform.parent);

			tn.transform.localScale = Vector3.one;
			tn.transform.localPosition = Vector3.zero;
			Vector2 om = tn.GetComponent<RectTransform> ().offsetMax;


			tn.GetComponent<RectTransform> ().offsetMax = new Vector2 (0, om.y);
			tn.GetComponent<RectTransform> ().offsetMin = new Vector2 (0, -om.y);

			tn.transform.FindChild ("t").GetComponent<Text> ().text = msg;
			_instance.tipHide (tn);

			_instance.tipTmpNode = tn;

			return true;
		}


		public void tipHide (GameObject tn)
		{
			float delayTime = 1.2f;
			float fadeTime = 0.6f;

			tn.GetComponent<Image> ().DOFade (0, fadeTime).SetDelay (delayTime).OnComplete (delegate () {
				tipTmpNode = null;
				GameObject.DestroyObject (tn);
			});

			tn.transform.FindChild ("t").GetComponent<Text> ().DOFade (0, fadeTime).SetDelay (delayTime);
		}


		public static void showLoaddingIndicate ()
		{
			if (_instance != null && _instance.loaddingIndicate != null)
				_instance.loaddingIndicate.show ();
		}

		public static void hideLoaddingIndicate ()
		{
			if (_instance != null && _instance.loaddingIndicate != null)
				_instance.loaddingIndicate.hide ();
		}

		// Update is called once per frame
		void Update ()
		{

		}
	}
}